package com.pine.app.module.security.oauth.provider.authrizationCode;

import com.pine.app.module.security.core.common.AuthConstant;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;

/**
 * @author xiaoyuan
 * @create 2019/10/23 15:49
 **/
public class RedisAuthenticationCodeStore implements AuthenticationCodeStore {

    private  static final  String OAUTH_CODE_CACHE_NAME= AuthConstant.CACHE_NAMESPACE+"CODE:";

    private RedisTemplate<String, AuthenticationCode> redisTemplate;

    private Long expireIn = 10L * 60L;

    public RedisAuthenticationCodeStore(RedisTemplate<String, AuthenticationCode> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public AuthenticationCode getByCode(String code) {
        return redisTemplate.opsForValue().get(OAUTH_CODE_CACHE_NAME+code);
    }

    @Override
    public void storeCode(AuthenticationCode oauthCode) {
        redisTemplate.opsForValue().set(OAUTH_CODE_CACHE_NAME+oauthCode.getCode(),oauthCode,expireIn, TimeUnit.SECONDS);
    }

    @Override
    public void removeByCode(String code) {
        redisTemplate.delete(OAUTH_CODE_CACHE_NAME+code);
    }
}
